const Router = require('koa-router');
const tool = require("../../tools/tool");
const router = new Router();
const {db, Op, QueryTypes} = require("../../models/db");
// 历史价格
router.post('/bill/getHistPrice', async ctx => {
    let user = ctx.user
    let body = ctx.request.body
    let tenantId = user.tenantId;
    let buId = body.buId;
    let invId = body.invId;
    let type = body.type;
    let bu = buId ? ` and buId=${buId} ` : ''
    let iv = invId ? ` and invId=${invId} ` : ''
    let sql = ``
    if (type === 'inPrice' || type === 'inPriceByBuId') {
        sql = `
        SELECT top 30 billDate,price,deduction,qty,unit from v_invoice_detail where 
        tenantId=${tenantId} ${bu} ${iv} and price <> 0 and billType='PUR' 
        ORDER BY id desc
        `
    }
    if (type === 'outPrice' || type === 'outPriceByBuId') {
        sql = `
        SELECT top 30 billDate,price,deduction,qty,unit from v_invoice_detail where 
        tenantId=${tenantId} ${bu} ${iv} and price <> 0 and billType='SALE' 
        ORDER BY id desc
        `
    }
    let ret = await db.query(sql, { type: QueryTypes.SELECT })
    return ctx.suc('', ret);

})

module.exports = router;